package com.passivator.logic;

import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

@MessageDriven(mappedName = "jms/PassivatorQueue", activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-Acknowledge")})
public class PassivatorProcessor implements MessageListener {

	private final static Logger logger = Logger
			.getLogger(PassivatorProcessor.class.getName());

	@EJB(name = "Passivator", mappedName = "Passivator")
	private Passivator passivator;

	@PostConstruct
	public void create() {
		logger.info("Instancia de PassivatorMDB creada");
	}

	public void onMessage(Message arg0) {

		try {
			ObjectMessage message = (ObjectMessage) arg0;
			byte[] session = (byte[]) message.getObject();

			boolean result = false;
			if (session != null) {
				// Invocamos al EJB
				result = passivator.passivate(session);
			}

		} catch (Exception ex) {

		}

	}

}
